<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ModuleCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'module:create  {--path=} {--name=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建模块命令';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        try {
            $path = $this->option('path');
            $name = $this->inputOption('name', '请输入功能目录名');
            $path = ucfirst($path);
            $name = ucfirst($name);

            // 创建主目录
            if ($path) {
                $mainPath = 'modules'. DIRECTORY_SEPARATOR . $path . DIRECTORY_SEPARATOR . $name;
            } else {
                $mainPath = 'modules' . DIRECTORY_SEPARATOR . $name;
            }
            mkdir(base_path($mainPath));

            // 创建业务目录
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'Controllers');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'routes');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'assets');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'components');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'js');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'sass');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'img');
            mkdir($mainPath . DIRECTORY_SEPARATOR . 'views');

            // 创建空白路由文件
            file_put_contents($mainPath . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'web.php', "<?php\n");

            $this->info('创建成功，模块目录：' . $mainPath);
            $this->info($this->successText($path, $name));

        } catch (\Exception $e) {
            $this->error('创建模块失败，错误详情：');
            $this->line($e);
        }
    }

    protected function inputOption($name, $text, $required = true)
    {
        if ($this->option($name)) {
            return $this->option($name);
        }
        $value = $this->ask($text);
        if (!$value && $required) {
            return $this->inputOption($name, $text, $required);
        }
        return $value;
    }

    protected function successText($path, $name)
    {
        $name = ($path ? $path . '\\' : '') . ucfirst($name);
        return <<<EOF
模块目录结构及文件说明：
1、~/Models/ 数据模型文件
2、~/Controllers/ 控制器文件
3、~/routes/web.php 路由
4、~/views/ 视图文件
5、~/assets/js/ 前端js文件
6、~/assets/sass/ 前端scss样式文件
7、~/assets/img/ 前端图片文件

接下来要做的事：
1、如果模块需要开发前端功能，请在根目录下webpack.mix.js中添加包名以便加入编译任务
2、路由和视图文件都需要在ServiceProvider中注册，您可以创建一个ServiceProvider文件并加下config/app.php的providers配置中
3、如果是Admin或Site目录下的子模块，需要把路由和视图在相应的provider中注册
4、注意此模块的命名空间是：Modules\\{$name}
5、开始coding...
EOF;
    }
}
